【学习】Spring Boot

本文还在持续更新中……

image-20200701205853317

> Spring Boot概述

1. 用途

  • 简化J2EE开发,去繁从简,简单创建独立的、企业级别的应用
  • Spring Boot用于一站式解决J2EE开发的方案

2. 优点

  • 快速创建独立运行的Spring项目以及主流框架集成
  • 使用嵌入式的Servlet容器,应用无需打包为WAR包
  • 自动依赖、自动配置,用户无需配置.xml,无代码生成,开箱即用
  • 与云计算进行天然集成

3. 微服务

微服务文档:https://martinfowler.com/articles/microservices.html

  • 是一种架构方式,提倡应用应该是一组小型服务的整合
  • 小型服务间通过HTTP等轻量方式进行通信

> QuickStart

1. 环境准备

2. Maven配置

settings.xml中的<profiles>标签里添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
<profile>
<id>jdk-14.0.1</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>14.0.1</jdk>
</activation>
<properties>
<maven.compiler.source>14.0.1</maven.compiler.source>
<maven.compiler.target>14.0.1</maven.compiler.target>
<maven.compiler.compilerVersion>14.0.1</maven.compiler.compilerVersion>
</properties>
</profile>

3. HelloWorld示例

(1) Maven配置以及依赖

创建Maven工程(jar),并且在pom.xml中导入Spring Boot相关依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.1.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

注意,由于未使用父项目,<version>必须要加上,否则Sync时会报错

(2) Spring Boot Application

开始一个主程序的编写,用于启动Spring Boot应用

1
2
3
4
5
6
7
8
9
10
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication //申明Spring Boot程序

public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args); //启动Spring应用

}
}

随后编写相关的业务逻辑(ControllerService),Controller的代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "HelloWorld";
}
}

(3) 启动程序

直接定位至main方法,运行Application,得到如下输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)

2020-07-02 11:26:26.909 INFO 16976 --- [ main] org.smooth.HelloWorldApplication : Starting HelloWorldApplication on Meme with PID 16976 (E:\IDEA_workfile\SpringTest\target\classes started by ASUS in E:\IDEA_workfile\SpringTest)
2020-07-02 11:26:26.913 INFO 16976 --- [ main] org.smooth.HelloWorldApplication : No active profile set, falling back to default profiles: default
2020-07-02 11:26:27.765 INFO 16976 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-07-02 11:26:27.779 INFO 16976 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-02 11:26:27.780 INFO 16976 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-02 11:26:27.879 INFO 16976 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-02 11:26:27.879 INFO 16976 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 907 ms
2020-07-02 11:26:28.031 INFO 16976 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-02 11:26:28.212 INFO 16976 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-02 11:26:28.230 INFO 16976 --- [ main] org.smooth.HelloWorldApplication : Started HelloWorldApplication in 1.759 seconds (JVM running for 3.173)
2020-07-02 11:27:27.682 INFO 16976 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-07-02 11:27:27.682 INFO 16976 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-07-02 11:27:27.687 INFO 16976 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms

从输出可以看到,Tomcat服务启动于8080端口,所以从浏览器访问localhost:8080,此时浏览器页面是一个错误页面,这是由于没有收到反馈,所以在地址后加上/hello表示传入“hello”,即访问:

1
localhost:8080/hello

可以看到浏览器页面上的内容变为了一行HelloWorld

(4) 部署你的应用

将以下代码导入到pom.xml中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

这段代码将一个Maven插件导入项目中,让应用打包为一个可执行的jar包,存放在target目录中

> 机制——以HelloWorld为例

1. Pom文件

(1) 父项目

1
2
3
4
5
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.1.RELEASE</version>
</parent>

其中spring-boot-starter-parent也有父项目,名为spring-boot-dependencies。它用来管理所有Spring Boot的依赖的版本,使得我们在pom.xml中不需要再对依赖注明版本,但也有部分包并没有列入其中的包,需要使用<version>tag来注明其版本

(4) Starters

在HelloWorld示例项目中,有如下代码

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>

其中,spring-boot-starter-web依赖于spring-boot-starter,后者是spring boot的场景启动器。它导入了使得web模块正常运行所依赖的组件

不仅如此,Spring Boot将所有的功能场景都抽取出来,比如用于数据处理的spring-boot-starter-redis依赖,这些starters就是Spring Boot中各种场景的启动器,我们仅需要在pom.xml中导入这些启动器,相关场景的所有依赖都会导入项目中

-------------FIN-------------

本文标题:【学习】Spring Boot

文章作者:吃草莓糖葫芦

发布时间:2020年07月02日 - 17:07

最后更新:2020年07月02日 - 17:07

原始链接:https://tsuinterukonsigure.github.io/2020/07/02/%E3%80%90%E5%AD%A6%E4%B9%A0%E3%80%91Spring%20Boot/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

如果文章对你有用,不妨捐助一下作者小哥哥叭~